/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.ProductSpecificationInterface;
import com.wii.elpro.dao.hb.ProductSpecificationImpl;
import com.wii.elpro.entity.ProductSpecification;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.ProductSpecificationUpdateForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class ProductSpecificationPreUpdateAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward;
        long id = Long.parseLong(request.getParameter("id"));
        ProductSpecificationInterface dao = new ProductSpecificationImpl();
        ProductSpecificationUpdateForm f = (ProductSpecificationUpdateForm) form;
        ActionMessages messages = null;        
        try {
            ProductSpecification ps = dao.getProductSpecification(id);
            f.setId(ps.getId());
            f.setKeterangan(ps.getKeterangan());
            f.setNama(ps.getNama());
            forward = "success";
        } catch (Exception ex) {
            messages = new ActionMessages();
            messages.add("message", new ActionMessage("customMessage", "Application is fail to retrieve data to update. Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            forward = "fail";
        }
        return mapping.findForward(forward);
    }
}
